home *** CD-ROM | disk | FTP | other *** search
/ PC Open 107 / PC Open 107 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / bayes.pl < prev    next >
Encoding:
Perl Script  |  2004-11-29  |  3.0 KB  |  105 lines

  1. #!/usr/bin/perl
  2. # ----------------------------------------------------------------------------
  3. #
  4. # bayes.pl --- Classify a mail message manually
  5. #
  6. # Copyright (c) 2001-2004 John Graham-Cumming
  7. #
  8. #   This file is part of POPFile
  9. #
  10. #   POPFile is free software; you can redistribute it and/or modify
  11. #   it under the terms of the GNU General Public License as published by
  12. #   the Free Software Foundation; either version 2 of the License, or
  13. #   (at your option) any later version.
  14. #
  15. #   POPFile is distributed in the hope that it will be useful,
  16. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. #   GNU General Public License for more details.
  19. #
  20. #   You should have received a copy of the GNU General Public License
  21. #   along with POPFile; if not, write to the Free Software
  22. #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #
  24. # ----------------------------------------------------------------------------
  25.  
  26. use strict;
  27. use lib defined($ENV{POPFILE_ROOT})?$ENV{POPFILE_ROOT}:'./';
  28. use POPFile::Loader;
  29.  
  30. my $code = 0;
  31.  
  32. if ( $#ARGV >= 0 ) {
  33.  
  34.     # POPFile is actually loaded by the POPFile::Loader object which does all
  35.     # the work
  36.  
  37.     my $POPFile = POPFile::Loader->new();
  38.  
  39.     # Indicate that we should create not output on STDOUT (the POPFile
  40.     # load sequence)
  41.  
  42.     $POPFile->debug(0);
  43.     $POPFile->CORE_loader_init();
  44.     $POPFile->CORE_signals();
  45.     $POPFile->CORE_load( 1 );
  46.     $POPFile->CORE_link_components();
  47.     $POPFile->CORE_initialize();
  48.  
  49.     my @files;
  50.  
  51.     if ($^O =~ /linux/) {
  52.         @files = @ARGV[0 .. $#ARGV];
  53.     } else {
  54.         @files = map { glob } @ARGV[0 .. $#ARGV];
  55.     }
  56.  
  57.     @ARGV = ();
  58.  
  59.     if ( $POPFile->CORE_config() ) {
  60.  
  61.         # Prevent the tool from finding another copy of POPFile running
  62.  
  63.         my $c = $POPFile->get_module('POPFile::Config');
  64.         $c->config_( 'piddir', $c->config_( 'piddir' ) . 'bayes.pl.' );
  65.  
  66.         # TODO: interface violation
  67.         $c->{save_needed__} = 0;
  68.  
  69.         $POPFile->CORE_start();
  70.  
  71.         my $b = $POPFile->get_module('Classifier::Bayes');
  72.         my $session = $b->get_session_key( 'admin', '' );
  73.  
  74.         foreach my $file (@files) {
  75.             if ( !(-e $file) ) {
  76.                 print STDERR "Error: File `$file' does not exist, classification aborted.\n";
  77.                 $code = 1;
  78.                 last;
  79.             }
  80.         }
  81.  
  82.         if ( $code == 0 ) {
  83.             foreach my $file (@files) {
  84.                 print "`$file' is `" . $b->classify( $session, $file ) . "'\n";
  85.             }
  86.  
  87.             foreach my $word (sort keys %{$b->{parser__}->{words__}}) {
  88.                 print "$word $b->{parser__}->{words__}{$word}\n";
  89.             }
  90.         }
  91.  
  92.         $b->release_session_key( $session );
  93.         $POPFile->CORE_stop();
  94.     }
  95. }
  96. else
  97. {
  98.     print "bayes.pl - output the classification of a message\n\n";
  99.     print "Usage: bayes.pl <messages>\n";
  100.     print "       <messages>         Filename of message(s) to classify\n";
  101.     $code = 1;
  102. }
  103.  
  104. exit $code;
  105.